const axios = require('axios')
const {logfn} = require("./logfn.js");
const schedule = require('node-schedule');

function getDouban(){
    axios.get('https://movie.douban.com/j/search_subjects?type=tv&tag=%E7%83%AD%E9%97%A8&page_limit=50&page_start=0')
    .then((response) => {
        console.log('success')
        logfn(response.data,'douban')
    })
    .catch((error) => {
        console.log('error')
    });
}

function getIciba(){
    var config = {
        method: 'get',
        url: 'http://open.iciba.com/dsapi/',
        headers: { 
          'Pragma': 'no-cache', 
          'Accept-Encoding': 'gzip, deflate', 
          'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8', 
          'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36', 
          'Accept': '*/*', 
          'Referer': 'http://demo1.zhengoma.cn/', 
          'Connection': 'keep-alive', 
          'Cache-Control': 'no-cache'
        }
      };
      
      axios(config)
      .then(function (response) {
        //console.log(JSON.stringify(response.data));
        //console.log(response.data);
        let data=response.data;
        // data.content 英文
        // data.note 中文
        // data.fenxiang_img 分享图

      })
      .catch(function (error) {
        console.log(error);
      });
}

function sendDing(content){
    var config = {
      method: 'post',
      url: 'https://oapi.dingtalk.com/robot/send?access_token=3fc5ceb52ffc5e6c1d9f6b25df851013f9124faa0a1821e71c376d3da7884bc5',
      headers: { 
        'Content-Type': 'application/json'
      },
        data : JSON.stringify({
            "msgtype":"text",
            "text":{
              "content":new Date().toLocaleString()+`  消息：\n`+content
            }
        })
    };
    
    axios(config)
    .then(function (response) {
      //console.log(response.data);
    })
    .catch(function (error) {
      console.log(error);
    });
}


async function getForum() {
    try {
        const response = await axios.get(
            'https://www.reddit.com/r/programming.json'
        )
        console.log(response)
    } catch (error) {
        console.error(error)
    }
}

function scheduleCancel(){

    var counter = 1;
    const j = schedule.scheduleJob('1 * * * * *', function(timez){ // 秒、分、时、日、月、周几
        //timez 2021-01-05T03:05:01.000Z   比北京时间小8小时
        console.log(timez.toString())
        console.log(timez.toTimeString())
        console.log(timez.toUTCString())
        if(counter>3){
            console.log('定时器取消')
            // 定时器取消
            j.cancel();
        }else{
            getIciba();
        }
        console.log('定时器触发次数：' + counter);
        counter++;
        
    });
}

// getIciba();
scheduleCancel();
// sendDing('哈哈哈');